package blog.db;

import javax.microedition.rms.RecordStoreException;

import blog.ConstString;
import blog.util.StringUtils;

public class BlogEntryDB {
	private static StorageConnectWithIndex store;
	private static String errorString;
	
	public static boolean openDB()
	{
		if ( store==null )
		{
			try {
				Class myclass;
				myclass = new BlogEntry().getClass();
				store = StorageConnectWithIndex.open(ConstString.DB_NAME_BLOG_ENTRY, true, myclass);
				store.setSortField(BlogEntry.FIELD_CREATED_DATE, StorageConnectWithIndex.SORT_TYPE_DESC);
			} catch (Exception e) {
				errorString = StringUtils.combin(ConstString.DB_ERR_COMMON_OPEN_FAILED, e.getMessage());
			}
		}
		return store==null;
	}
	
	public static void delete( int accountId )
	{
		if ( store!=null )
		{
			IStorableRecordWithIndex[] result;
			result = store.searchIndex( BlogEntry.FIELD_ACCOUNT_ID, new Integer(accountId) );
			for( int i=0; i<result.length; i++ )
			{
				try
				{
					store.delete( result[i] );
				}
				catch (RecordStoreException e)
				{
				}
			}
		}
	}
	
	public static void closeDB()
	{
		if ( store!=null )
		{
			store.close();
			store = null;
		}
	}
	
	public static String getLastError()
	{
		return errorString;
	}
	
	public static boolean add( BlogEntry entry )
	{
		if ( store!=null )
		{
			try {
				store.add(entry);
				return true;
			} catch (Exception e) {
				errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
			}
		}
		return false;
	}
	
	public static boolean update( BlogEntry entry )
	{
		if ( store!=null )
		{
			try {
				store.update(entry);
				return true;
			} catch (Exception e) {
				errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
			}
		}
		return false;
	}
	
	public static boolean delete( BlogEntry entry )
	{
		try {
			store.delete(entry);
			return true;
		} catch (Exception e1) {
			errorString = ConstString.DB_ERR_COMMON_OPERATION_FAILED;
		}
		return false;
	}
	
	public static BlogEntry getById( int id, int indexId )
	{
		BlogEntry Entry = (BlogEntry)store.getById(id, indexId);
		if ( Entry==null )
		{
			store.fix();
			Entry = (BlogEntry)store.getById(id, indexId);
		}
		return Entry;
	}
	
	public static BlogEntry[] getIndex( int accountId, boolean publishOk )
	{
		IStorableRecordWithIndex[] result;
		try {
			result = store.searchIndex( new int[]{BlogEntry.FIELD_ACCOUNT_ID, BlogEntry.FIELD_PUBLISH_OK}, new Object[]{new Integer(accountId), new Boolean(publishOk)} );
			if ( result.length>0 )
			{
				BlogEntry[] returnValue = new BlogEntry[ result.length ];
				System.arraycopy(result, 0, returnValue, 0, result.length);
				return returnValue;
			}
		} catch (Exception e) {
		}
		return new BlogEntry[0];
	}
	
	public static int count( int accountId, boolean publishOk )
	{
		IStorableRecordWithIndex[] result;
		try {
			result = store.searchIndex( new int[]{BlogEntry.FIELD_ACCOUNT_ID, BlogEntry.FIELD_PUBLISH_OK}, new Object[]{new Integer(accountId), new Boolean(publishOk)} );
			return result.length;
		} catch (Exception e) {
		}
		return 0;
	}
	
//	public static BlogEntry[] get( int accountId, boolean publishOk )
//	{
//		IStorableRecordWithIndex[] result;
//		try {
//			result = store.search( new int[]{BlogEntry.FIELD_ACCOUNT_ID, BlogEntry.FIELD_PUBLISH_OK}, new Object[]{new Integer(accountId), new Boolean(publishOk)} );
//			if ( result.length>0 )
//			{
//				BlogEntry[] returnValue = new BlogEntry[ result.length ];
//				System.arraycopy(result, 0, returnValue, 0, result.length);
//				return returnValue;
//			}
//		} catch (Exception e) {
//		}
//		return new BlogEntry[0];
//	}
	
	public static void setMaxSize( int accountId, int num, boolean publishOk )
	{
		IStorableRecordWithIndex[] result;
		try
		{
			result = store.searchIndex( new int[]{BlogEntry.FIELD_ACCOUNT_ID, BlogEntry.FIELD_PUBLISH_OK}, new Object[]{new Integer(accountId), new Boolean(publishOk)} );
			if ( result.length>num )
			{
				for( int i=num; i<result.length; i++ )
				{
					store.delete(result[i]);
				}
			}
		}
		catch( Exception e)
		{
		}
	}
}
